package com.common.operationLog;


import com.common.operationLog.controller.OperationLogController;
import com.common.operationLog.pojo.bo.OperationLogContext;
import com.common.operationLog.pojo.bo.OperationLogUserInfoContext;
import com.common.operationLog.service.IOperationLogService;
import com.common.operationLog.service.impl.OperationLogServiceImpl;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;

import java.util.Collections;
import java.util.List;

/**
 * 操作日志模块自动导入配置
 */
@AutoConfiguration
@MapperScan("com.common.operationLog.mapper")
public class OperationLogAutoConfiguration {

    /**
     * 调用方需要进行 OperationLogConfigurationInfo 配置, 覆盖默认空的配置
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public OperationLogContext operationLogContext() {
        OperationLogContext operationLogContext = new OperationLogContext();
        // 设置枚举列表-用于下拉框
        operationLogContext.setOperationLogModuleTypeList(Collections.emptyList());
        // 设置用户信息上下文 若保存日志时未设置操作人信息，将从用户信息上下文中获取
        operationLogContext.setOperationLogUserInfoContextSupplier(() -> {
            OperationLogUserInfoContext operationLogUserInfoContext = new OperationLogUserInfoContext();
            operationLogUserInfoContext.setOperationBy(null);
            operationLogUserInfoContext.setOperationByName(null);
            return operationLogUserInfoContext;
        });
        return operationLogContext;
    }

    @Bean
    @ConditionalOnMissingBean
    public IOperationLogService operationLogService() {
        return new OperationLogServiceImpl();
    }

    @Bean
    @ConditionalOnMissingBean
    public OperationLogController operationLogController() {
        return new OperationLogController();
    }

}
